package cn.yogehaoren.foxgitstarter.runner;

import cn.yogehaoren.foxgitstarter.properties.GitProperties;
import cn.yogehaoren.foxgitstarter.git.FoxGitService;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.jgit.lib.Repository;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;

/**
 * @author WangNing
 * @since 1.0
 */

@Slf4j
public class GitBookInitRunner implements ApplicationRunner{

    private final FoxGitService gitService;

    private final GitProperties gitProperties;

    public GitBookInitRunner(FoxGitService foxGitService, GitProperties gitProperties){
        this.gitService = foxGitService;
        this.gitProperties = gitProperties;
    }



    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("开始同步数据");
        Repository repository = gitService.getRepository();
        if(repository==null){
            log.info("本地未发现文件,开始Clone仓库");
            gitService.cloneRepository();
            if(gitProperties.getCloseSystemOnFirstClone()){
                log.info("clone完毕 重启系统");
                System.exit(0);
            }
        }else {
            repository.close();
            if(gitProperties.getForcePull()){
                gitService.forcePullRepository();
            }else {
                gitService.pullRepository();
            }

        }
        log.info("数据同步完毕");
    }
}
